package com.kung.qvs.ui.main.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.Navigation;

import com.codbking.widget.DatePickDialog;
import com.codbking.widget.OnSureLisener;
import com.codbking.widget.bean.DateType;
import com.kung.qvs.BR;
import com.kung.qvs.R;
import com.kung.qvs.app.AppViewModelFactory;
import com.kung.qvs.databinding.FragCollectTrashBinding;
import com.kung.qvs.entity.KRecordWeight;
import com.kung.qvs.entity.PrintDataBean;
import com.kung.qvs.entity.database.SqlManager;
import com.kung.qvs.ui.main.HomeActivity;
import com.kung.qvs.ui.main.KungViewModel;
import com.kung.qvs.ui.widget.ConformDialog;
import com.kung.qvs.utils.DateUtils;

import java.util.Date;

import me.goldze.mvvmhabit.base.BaseFragment;

/**
 * 收集的垃圾记录
 */
public class CollectTrashRecordFrag extends BaseFragment<FragCollectTrashBinding,CollectTrashRecordViewModel> {

    private KungViewModel kungViewModel;
    private HomeActivity homeActivity;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.frag_collect_trash;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public CollectTrashRecordViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(getActivity(), factory).get(CollectTrashRecordViewModel.class);
    }

    @Override
    public void initParam() {
        super.initParam();
    }

    @Override
    public void initData() {
        super.initData();
        homeActivity = (HomeActivity) getActivity();
        kungViewModel = new ViewModelProvider(getActivity()).get(KungViewModel.class);
        kungViewModel.setmIsHomeFrag(false);

        viewModel.doGetTrashDataBeans();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.eventToBack.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                //返回上一级
                Navigation.findNavController(getView()).navigateUp();
            }
        });

        viewModel.uc.eventPrintItem.observe(this, new Observer<KRecordWeight>() {
            @Override
            public void onChanged(KRecordWeight dataBean) {
                ConformDialog.Builder builder = new ConformDialog.Builder(getActivity());
                builder.setMessage("是否打印称重记录？", "");
                builder.setTitle("打印称重");
                builder.setPositiveButton("确认",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog2,
                                                int which) {
                                dialog2.dismiss();
                                //打印
                                PrintDataBean printDataBean = new PrintDataBean();
                                printDataBean.setCompanyName(dataBean.getCompanyName());
                                printDataBean.setProductId(dataBean.getRecordId());
                                printDataBean.setProductName(dataBean.getProductName());
                                printDataBean.setStandardWeight(dataBean.getSpecification());
                                printDataBean.setWeight(dataBean.getWeight());
                                printDataBean.setAmount(dataBean.getAmount());
                                printDataBean.setWorkId(dataBean.getUserName());
                                printDataBean.setWorkName(dataBean.getUserNikename());
                                printDataBean.setFcdate(dataBean.getFcdate());
                                printDataBean.setPrintCode(dataBean.getRecordId());
                                printDataBean.setListing(dataBean.getListing());

                                homeActivity.doPrintContent(printDataBean, "");
                            }
                        });

                builder.setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog2,
                                                int which) {
                                dialog2.dismiss();
                            }
                        });
                builder.create().show();
            }
        });

        viewModel.uc.eventStartSync.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                homeActivity.aotoUploadTrashRecord();
            }
        });

        viewModel.uc.eventDateStart.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                DatePickDialog dialog = new DatePickDialog(getContext());
                //设置上下年分限制
                dialog.setYearLimt(5);
                //设置标题
                dialog.setTitle("选择开始时间");
                //设置类型
                dialog.setType(DateType.TYPE_YMD);
                //设置消息体的显示格式，日期格式
                dialog.setMessageFormat("yyyy-MM-dd HH:mm");
                //设置选择回调
                dialog.setOnChangeLisener(null);
                //设置点击确定按钮回调
                dialog.setOnSureLisener(new OnSureLisener() {
                    @Override
                    public void onSure(Date date) {
                        viewModel.mStartDate.set(DateUtils.dataToStringDate(date));
                    }
                });
                dialog.show();
            }
        });

        viewModel.uc.eventDateEnd.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                DatePickDialog dialog = new DatePickDialog(getContext());
                //设置上下年分限制
                dialog.setYearLimt(5);
                //设置标题
                dialog.setTitle("选择结束时间");
                //设置类型
                dialog.setType(DateType.TYPE_YMD);
                //设置消息体的显示格式，日期格式
                dialog.setMessageFormat("yyyy-MM-dd HH:mm");
                //设置选择回调
                dialog.setOnChangeLisener(null);
                //设置点击确定按钮回调
                dialog.setOnSureLisener(new OnSureLisener() {
                    @Override
                    public void onSure(Date date) {
                        viewModel.mEndDate.set(DateUtils.dataToStringDate(date));
                    }
                });
                dialog.show();
            }
        });

        //监听下拉刷新完成
        viewModel.uc.finishRefreshing.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {
                viewModel.doGetTrashDataBeans();
                //结束刷新
                binding.recyclingRL.finishRefreshing();
            }
        });
        //监听上拉加载完成
        viewModel.uc.finishLoadmore.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {
                //结束刷新
                viewModel.doGetTrashDataBeans();
                binding.recyclingRL.finishLoadmore();
            }
        });
    }
}
